Skip to content

Add formatting of small floats (10^-n)#319

Merged
tolik518 merged 2 commits intotolik518:masterfrom
Aras14HD:format-float-small
Feb 26, 2026
Merged

Add formatting of small floats (10^-n)#319
tolik518 merged 2 commits intotolik518:masterfrom
Aras14HD:format-float-small

Conversation

@Aras14HD
Copy link
Collaborator

This enables formatting small floats like 6.631537423e-34 as 6.631537423 × 10^-34, by allowing negative exponents in format_float (and some more logic to handle that).

Also fixes #315

Resolves #308

also fixes incorrect rounding
@tolik518
Copy link
Owner

tolik518 commented Feb 24, 2026

On your test-run factorion replied on 6.631537423e-34! with Factorial of 6.631537423 × 10-34 is approximately 1, before that it was replying with Factorial of 0.000000000000000000000000663154 is approximately 0,999999999999999999999999617217.

https://www.reddit.com/r/testingground4bots/comments/1rabfbh/comment/o77a0e8/

The before reply was more precise

@Aras14HD
Copy link
Collaborator Author

Ok investigating this I found, that the true result is 9.999999999999999999999999999999996172172717063657224823672597116605474397837555828144330523967376628717255627642017055031081750434013261087338778618778539329115394750197229780734699344408893837837414084874090262554352460558789098273872863113806646310596473029774819683214882891955307493169687938970352854185035e-1 not 0.999999999999999999999999617217. That's a lot more 9s in reality (more than 30).

Even though I confirmed we now do better (as more correct), I wanna figure out why it was wrong previously.

@Aras14HD
Copy link
Collaborator Author

Checked on master now, and can't reproduce that response, just get 0.1. What options (consts like float_precision) did you use?

@tolik518
Copy link
Owner

Checked on master now, and can't reproduce that response, just get 0.1. What options (consts like float_precision) did you use?

I needed a while to reproduce, but it happens to me only if the language is NOT en.

Example-test:

#[test]
fn teststuff() {
    let consts = Consts::default();
    let comment = Comment::new(
        "6.631537423e-34! ",
        (),
        Commands::TERMIAL,
        MAX_LENGTH,
        "de",
    )
        .extract(&consts)
        .calc(&consts);

    let reply = comment.get_reply(&consts);
    assert_eq!(
        reply,
        "Fakultät von 0,000000000000000000000000663154 ist ungefähr 0,999999999999999999999999617217 \n\n\n*^(Dieser Kommentar wurde automatisch geschrieben | [Quelltext](http://f.r0.fyi))*"
    );
}

for en the test would fail, for en the reply would have been Factorial of 0 is approximately 0.1 \n\n\n*^(This action was performed by a bot | [Source code](http://f.r0.fyi))* as you stated.

@Aras14HD
Copy link
Collaborator Author

Oh, It's not doing the same calculation at all. Since the decimal separator is ',' and not '.' the latter is simply ignored.

So on en we calculate the factorial of 6.631537423e-34, but on ru and de, we calculate the factorial of 6631537423e-34.

I am unsure on what should be done. To "fix" this we would have to somehow find, when it is not used as a separator, specifying some pattern of separators per locale (char and interval?) and then? Treat it as a decimal separator or give an error or something?

@tolik518
Copy link
Owner

Ohh, right! That was my bad!
Its basically 0.0000000000000000000000000000000006631537423 vs 0.0000000000000000000000006631537423.

This is whole other issue as it appears. We would need a heuristic approach that makes assumptions about the separator.

I can merge the PR later in the evening, as it seems to work as intended to me.

@tolik518 tolik518 merged commit 51a8b41 into tolik518:master Feb 26, 2026
6 checks passed
@Aras14HD Aras14HD deleted the format-float-small branch February 27, 2026 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rounding error on 0.9999... Very small float formatting

2 participants